/**
 * 
 */
package monitor;

import java.awt.EventQueue;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.beans.PropertyChangeSupport;
import java.util.UUID;

/**
 * @author Robbie
 *
 */
public class Analyzer extends Thread implements PropertyChangeListener {

	private String myPacketID;
	private String myFieldName;
	
	private PropertyChangeSupport listeners = new PropertyChangeSupport(this);

	public Analyzer(String pktID, String field){
		if(pktID.length()!=8){
			try {
				throw new Exception("Packet ID is not 8 characters");
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		myPacketID = pktID;
		myFieldName = field;
	}
	
	// This method should be made more extensible
	protected void broadcastFieldUpdate(UUID uuid, String data){
		PropertyChangeEvent e = new PropertyChangeEvent(this,myFieldName, uuid, data);
		scheduleUpdate(e);
	}
	
	public String getPacketID(){
		return new String(myPacketID);
	}
	
	public String getFieldName(){
		return new String(myFieldName);
	}
	
	
	/**
	 * Adds a property change listener
	 * 
	 * @param l listener to add
	 */
	public synchronized void addPropertyChangeListener(PropertyChangeListener l) {
	    listeners.addPropertyChangeListener(l);
	}
	
	/**
	 * Removes a property change listener
	 * 
	 * @param l listener to remove
	 */
	public synchronized void removePropertyChangeListener(PropertyChangeListener l) {
	    listeners.removePropertyChangeListener(l);
	}
	
	private void fireUpdate(PropertyChangeEvent evt) {
	    listeners.firePropertyChange(evt);
	}


	private void scheduleUpdate(final PropertyChangeEvent evt) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				fireUpdate(evt);
			}
		});
	}
	
	/**
	 * This method should be overridden by child classes.
	 */
	public void propertyChange(PropertyChangeEvent evt) {
		// TODO Auto-generated method stub

	}

}
